﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WellBuilt.Model;

namespace WellBuilt.Service
{
    public class GirderService
    {
        internal static GirderOut GirderResult(GirderIn girderIn)
        {
            //强度验算结果=IF(F17<F5,"强度OK","重新选梁")
            double B5 = girderIn.P1;
            double B15 = girderIn.A;
            double I5 = girderIn.LoadType == "1个集中荷载" ? girderIn.L0_1 : girderIn.L0_2;
            double C15 = I5 - B15;
            double G11 = 0;
            double G12 = 0;
            double C11 = 0;
            double C12 = 0;
            double D11 = 0;
            double D12 = 0;
            double E11 = 0;
            double E12 = 0;
            double I11 = 0;
            double I12 = 0;
            double G16 = 0;
            switch (girderIn.ISteel)
            {
                case 20:
                    G11 = 0.311;
                    G12 = 0.311;
                    C11 = 250;
                    C12 = 250;
                    D12 = 33.1;
                    D11 = 33.1;
                    E11 = 2500;
                    E12 = 2500;
                    I11 = 16.9;
                    I12 = 16.9;
                    G16 = 9;
                    break;
                case 22:
                    G11 = 0.365;
                    G12 = 0.365;
                    C11 = 325;
                    C12 = 325;
                    D12 = 42.7;
                    D11 = 42.7;
                    E11 = 3570;
                    E12 = 3570;
                    I11 = 18.7;
                    I12 = 18.7;
                    G16 = 9.5;
                    break;
                case 25:
                    G11 = 0.42;
                    G12 = 0.42;
                    C11 = 423;
                    C12 = 423;
                    D12 = 52.4;
                    D11 = 52.4;
                    E11 = 5280;
                    E12 = 5280;
                    I11 = 21.3;
                    I12 = 21.3;
                    G16 = 10;
                    break;
                case 28:
                    G11 = 0.48;
                    G12 = 0.48;
                    C11 = 534;
                    C12 = 534;
                    D12 = 61.2;
                    D11 = 61.2;
                    E11 = 7480;
                    E12 = 7480;
                    I11 = 24.2;
                    I12 = 24.2;
                    G16 = 10.5;
                    break;
                case 63:
                    G11 = 1.313;
                    C11 = 3160;
                    D11 = 204;
                    E11 = 98100;
                    I11 = 53.5;
                    G16 = 15;
                    break;
                case 56:
                    G11 = 1.151;
                    C11 = 2450;
                    D11 = 174;
                    E11 = 68500;
                    I11 = 47.3;
                    G16 = 14.5;
                    break;
                case 50:
                    G11 = 1.015;
                    C11 = 1940;
                    D11 = 146;
                    E11 = 48600;
                    I11 = 42.4;
                    G16 = 14;
                    break;
                case 45:
                    G11 = 0.875;
                    C11 = 1500;
                    D11 = 118;
                    E11 = 33800;
                    I11 = 38;
                    G16 = 13.5;
                    break;
                case 32:
                    G11 = 0.577;
                    C11 = 726;
                    D11 = 76;
                    E11 = 11600;
                    I11 = 27.1;
                    G16 = 11.5;
                    break;
                case 36:
                    G11 = 0.657;
                    C11 = 919;
                    D11 = 84.3;
                    E11 = 16500;
                    I11 = 30.3;
                    G16 = 12;
                    break;
                case 40:
                    G11 = 0.739;
                    C11 = 1140;
                    D11 = 96.2;
                    E11 = 22800;
                    I11 = 33.6;
                    G16 = 12.5;
                    break;
                default:
                    G12 = 0;
                    break;
            }
            double G8 = 1.4;
            double F15 = girderIn.LoadType == "1个集中荷载" ? (B5 * B15 * C15 / I5 + G11 * Math.Pow(I5, 2) / 8) * 1.05 / 1000 * G8 : 0;
            double E8 = 1.05;
            double B8 = girderIn.P1_1;
            double H15 = girderIn.LoadType == "1个集中荷载" ? B8 * B15 * C15 * 1.05 / I5 / 1000 * G8 : 0;
            double F8 = 1.2;
            double D15 = girderIn.A1;
            double C5 = girderIn.P2;
            double D5 = girderIn.P3;
            double E15 = girderIn.B1;
            double G15 = girderIn.LoadType == "2个集中荷载" ? ((D15 * C5 + (D15 + E15) * D5) * (I5 - D15) / I5 - D5 * E15 + G11 * Math.Pow(I5, 2) / 8) * 1.05 / 1000 * G8 : 0;
            double C8 = girderIn.P2_1;
            double D8 = girderIn.P3_1;
            double I15 = girderIn.LoadType == "2个集中荷载" ? ((D15 * C8 + (D15 + E15) * D8) * (I5 - D15) / I5 - D8 * E15) * 1.05 / 1000 * G8 : 0;
            double F17 = girderIn.LoadType == "1个集中荷载" ? (F15 / C11 / E8 + H15 / D11 / F8) * 1000000 : (G15 / C11 / E8 + I15 / D11 / F8) * 1000000;
            string gcph = girderIn.SteelMarks;
            double F5 = gcph == "Q235" ? 205 : (gcph == "Q345" ? 295 : 335);

            //传出参数
            GirderOut girderOut = new GirderOut();
            girderOut.QD_Checking = F17 < F5 ? "强度OK" : "重新选梁";
            girderOut.QD_Calculated = F17;
            girderOut.QD_Admissible = F5;
            //整体稳定性=IF(F19<F5,"稳定性OK","重新选梁")
            double I8 = girderIn.Factor;
            double F19 = girderIn.LoadType == "1个集中荷载" ? (F15 / C11 / I8 + H15 / D11 / F8) * 1000000 : (G15 / C11 / I8 + I15 / D11 / F8) * 1000000;
            girderOut.WDX_Checking = F19 < F5 ? "稳定性OK" : "重新选梁";
            girderOut.WDX_Calculated = F19;
            girderOut.WDX_Admissible = F5;
            //挠度=IF(D22<I22,"挠度OK","重新选梁")
            double I16 = 1.05 * I5;
            double H5 = 206000;
            double F21 = girderIn.LoadType == "1个集中荷载" ? B5 * Math.Pow(I16, 3) / 48 / H5 / E11 * 100000 * G8 : G8 * C5 * D15 * ((D15 + I16 - E15) * Math.Pow(I16, 2) - 4 * Math.Pow(D15, 2) * I16 + 2 * Math.Pow(D15, 3) - Math.Pow(D15, 2) * (I16 - D15 - E15) - Math.Pow((I16 - D15 - E15), 3)) * 100000 / (6 * H5 * E11 * I16);
            double D22 = F21 / I16;
            double I22 = girderIn.Deflection == "1/400" ? 0.0025 : 0.002;
            girderOut.ND_Checking = D22 < I22 ? "挠度OK" : "重新选梁";
            girderOut.ND_Calculated = D22;
            girderOut.ND_Admissible = I22;
            //抗剪强度=IF(F27<G5,"抗剪OK","重新选梁")
            double F24 = girderIn.LoadType == "1个集中荷载" ? B5 * B15 / I5 * G8 : 0;
            double F25 = girderIn.LoadType == "2个集中荷载" ? C5 * (2 * D15 + E15) * G8 / I5 : 0;
            double F26 = G11 * I5 * G8 / 2;
            double G5 = gcph == "Q235" ? 120 : (gcph == "Q345" ? 170 : 190);
            double F27 = girderIn.LoadType == "1个集中荷载" ? (F24 + F26) / I11 / G16 * 100 : (F25 + F26) / I11 / G16 * 100;
            girderOut.KJ_Checking = F27 < G5 ? "抗剪OK" : "重新选梁";
            girderOut.KJ_Calculated = F27;
            girderOut.KJ_Admissible = G5;

            return girderOut;
        }
    }
}
